package com.PQLoad
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	public class SwfLoader extends Load{
		private var bytedata:ByteArray;
		private var completeParam:Object;
		public var child:DisplayObject;
		public function SwfLoader(_url:String, param:Object=null){
			load=new Loader();
			super(_url, param);
			urlload.dataFormat = URLLoaderDataFormat.BINARY;
			urlload.load(new URLRequest(url));
			type=1;
		}

		/**该数据需要等资源加载完毕后方可取值**/
		public function get data():ByteArray{
			return bytedata;
		}
		/**获取对象**/
		public function copy(callBack:Function,param:Object=null):void{
			onComplete=callBack;
			completeParam=param;
			loadByte();
		}
		/**获取当前SWF库中的类**/
		public function getClassByLibrary(name:String):Class{
			var cl:Class;
			try{
				cl=load.contentLoaderInfo.applicationDomain.getDefinition(name) as Class;
			}catch(err){
				trace("url:"+url+"中没有找到"+name+"的类");
			}
			return cl;
		}
		/**获取通过Embed嵌入的类**/
		public function getClassByEmbed(name:String):Class{
			return child[name] as Class;
		}
		override protected function loaded(e:Event):void{
			bytedata=urlload.data;
			loadByte();
			removeListen();
		}
		private function loadByte():void{
			load.contentLoaderInfo.addEventListener(Event.COMPLETE,byteloaded);
			load.loadBytes(bytedata);
		}
		private function byteloaded(e:Event):void{
			child=e.target.content;
			addChild(child);
			removeByteListen();
			if(!onComplete)return;
			if(completeParam)onComplete(child,completeParam);
			else onComplete(child);
			removeFunc();
		}
		private function removeByteListen():void{
			if(load)load.contentLoaderInfo.removeEventListener(Event.COMPLETE,byteloaded);
		}
	}
}